<?php
/**
 * 首页显示
 */
class IndexControl extends CommonControl
{

    /**
     * 首页显示
     * @return [type] [description]
     */
    public function index()
    {
        $db = M("g_category");
        $category = Data::channelLevel($db->field("cid,pid,cname")->all());
        $this->assign("category", $category);
        $this->display("index");
    }


    /**
     * 商品列表页
     * @return [type] [description]
     */
    public function category()
    {
        $cid = Q("cid", "intval");
        if ($cid) {
            $this->formatUrl($cid);
        }
        $cidArr = explode('-', $_GET['s']);
        $cid = $cidArr[0];
        $db = M('g_goods');
        $count = $db->where('category_cid='.$cid)->count();
        $this->assign('count',$count);
        //获取父级栏目
        $pidData = $db->table('g_category')->all();
        $data = Data::parentChannel($pidData,$cid);
        $data = array_reverse($data);
        $this->assign('pidData',$data);
        //获取子级栏目
        $id = $data[1]['cid'];
        $catData = $db->table('g_category')->where('level=1')->where('cid='.$id)->all();
        foreach ($catData as $k => $v) {
            $catData[$k]['childData'] = $db->table('g_category')->where('level=2')->where('pid='.$id)->all();
            foreach ($catData[$k]['childData'] as $kk => $vv) {
                $catData[$k]['childData'][$kk]['count'] = $db->table('g_goods')->where('category_cid='.$vv['cid'])->count();
            }
        }
        $this->assign('catData',$catData);
        //分配栏目的属性
        $this->assignCategoryAttr();
        //获得栏目列表页的商品
        $this->assignCategoryGoods();
        $this->display('list');
    }


    /**
     * 改变url地址栏
     * @param  [type] $cid [description]
     * @return [type]      [description]
     */
    private function formatUrl($cid)
    {
        $category = M("g_category")->find($cid);
        $gt_id=$category['goods_type_gt_id'];
        $attr_class = M("g_attr_class")->where("goods_type_gt_id=".$gt_id )->all();
        $url = __METH__ . "/s/" . $cid . "-";
        foreach ($attr_class as $a) {
            $url .= "0-";
        }
        $url = substr($url, 0, -1) . C("PATHINFO_HTML");
        go($url);
    }

        //获得栏目列表页的商品
    private function assignCategoryGoods()
    {
        $querySearch = explode("-", q("s"));
        $cid = array_shift($querySearch); //栏目cid
        $searchAttr = array();
        foreach ($querySearch as $s) {
            if ($s) $searchAttr[] = $s;
        }
        $db = M("g_goods");
        if (empty($searchAttr)) {
            $sql = "SELECT count(*) AS c FROM hd_g_goods WHERE category_cid=$cid";
            $count = M()->query($sql);
            $page = new Page($count[0]['c'], 10);
            $this->assign("page", $page->show()); //分配页码
            $sql = "SELECT g.gname,g.list_pic,s.price,s.st_id FROM hd_g_goods AS g JOIN hd_g_stock AS s
             ON g.gid = s.goods_gid WHERE g.category_cid=$cid GROUP BY g.gid LIMIT " . $page->limit(true);
            $goods = $db->query($sql);
            $this->assign("goods", $goods); //分配商品数据
        } else {
            $from = " FROM hd_g_goods AS g JOIN ";
            //组织各表的关联   商品表与商品属性表的自连接
            foreach ($searchAttr as $av_id) {
                $from .= "hd_g_goods_attr AS t{$av_id} JOIN ";
            }
            $from = substr($from, 0, -5);
            $on = " ON "; //ON关联条件
            foreach ($searchAttr as $av_id) {
                $on .= " t{$av_id}.goods_gid=g.gid AND ";
            }
            //where条件
            $on = substr($on, 0, -4);
            $where = " WHERE ";
            foreach ($searchAttr as $av_id) {
                $where .= "t{$av_id}.attr_value_av_id={$av_id} AND ";
            }
            $where = substr($where, 0, -4) . " AND g.category_cid=$cid";
            $countSql = "SELECT count(*) AS c " . $from . $on . $where;
            $count = M()->query($countSql);
            //加ON
            $page = new Page($count[0]['c']);
            $this->assign("page", $page);
            $from .= " JOIN hd_g_stock as gs";
            $on .= " AND gs.goods_gid=g.gid";
            $sql = "SELECT g.price,g.gname,g.click,g.list_pic,gs.st_id" . $from . $on . $where . " GROUP BY g.gid LIMIT " . $page->limit(true);
            $goods = M()->query($sql);
            $this->assign("goods", $goods);
        }
    }



    /**
     * 获取当前栏目列表页属性
     * @return [type] [description]
     */
    private function assignCategoryAttr()
    {
        $s = explode("-", q("s"));
        $cid = current($s); //获得栏目cid
        $category = M("g_category")->find($cid);
        //获得栏目所属商品类型的所有属性
        $attr_class = M("g_attr_class")->where("goods_type_gt_id={$category['goods_type_gt_id']} and is_spec=0")->all();
        $attr = array();
        $db = M("g_attr_value"); //属性值表
        foreach ($attr_class as $a) {
            $a['attr_value'] = $db->where("attr_class_ac_id={$a['ac_id']}")->all();
            $attr[] = $a;
        }
        $this->assign("search_attr", $attr);
    }

    /**
     * 商品详情页
     * @return [type] [description]
     */
    public function goods()
    {
        $st_id = q("st_id", "intval");
        $db = M();
        $sql = "SELECT g.gname,g.gid,s.price,g.body,g.keyworkds,g.category_cid,g.description,g.body,g.service,g.click,g.click,
         s.st_id,s.stock
         FROM hd_g_stock AS s JOIN hd_g_goods AS g
         ON s.goods_gid=g.gid WHERE st_id=$st_id";
        $goods = $db->query($sql);
        $goods = current($goods);
        //获得该商品的属性类型
        $sql = "SELECT ac.ac_id,ac.attr_name,ac.is_spec FROM hd_g_category AS c JOIN hd_g_attr_class AS ac
        ON c.goods_type_gt_id = ac.goods_type_gt_id
        WHERE c.cid = {$goods['category_cid']} AND ac.is_spec = 1";
        $attr_class = $db->query($sql);
        $attr = array();
        foreach ($attr_class as $a) {
            $sql = "SELECT * FROM hd_g_attr_value AS av JOIN hd_g_goods_attr AS ga
             ON av.av_id = ga.attr_value_av_id
             WHERE av.attr_class_ac_id = {$a['ac_id']} AND ga.goods_gid={$goods['gid']}";
            $a['value'] = $db->query($sql);
            $attr[] = $a;
        }
        $goods['g_attr'] = $attr;
        $goods_gid = $attr[0]['value'][0]['goods_gid'];
        $this->assign("goods_spec", $this->getGoodsAttrId($st_id));
        $this->assign("goods", $goods); 
        //放大镜调用 ↓
        $this->magnifier($goods_gid);
        //获取当前栏目和父栏目
        $this->getCategory($goods_gid);
        $this->display('shop');
    }

    //获取当前商品规格
    private function getGoodsAttrId($st_id)
    {
        $db = M("g_stock_attr");
        //获得当前库存商品的规格id   红1 x2  获得array(1,2)
        $av_id = $db->field("attr_value_av_id")->where("stock_st_id=$st_id")->all();
        $id = array();
        foreach ($av_id as $a) {
            $id[] = $a['attr_value_av_id'];
        }
        return $id;
    }

    //商品内容页，点击规格后，获得库存商品的st_id
    public function getStockId()
    {
        $st_id = q("post.st_id", "intval");
        //查找商品gid  goods表
        $gid = M("g_stock")->field("goods_gid")->find($st_id);
        $sql = " FROM ";
        $av_id = q("post.av_id");
        foreach ($av_id as $id) {
            $sql .= "hd_g_stock_attr AS t{$id} JOIN ";
        }
        $sql = substr($sql, 0, -5);
        //1,2
        //ON
        $sql .= " ON ";
        for ($i = 0; $i < (count($av_id) - 1); $i++) {
            $sql .= " t{$av_id[$i]}.stock_st_id =  t{$av_id[$i + 1]}.stock_st_id AND";
        }
        //WHERE
        $sql = substr($sql, 0, -4) . " WHERE ";
        foreach ($av_id as $id) {
            $sql .= "t{$id}.attr_value_av_id=$id AND ";
        }
        $sql = $sql . " t{$av_id[$i]}.goods_gid={$gid['goods_gid']}";
        $sql = "SELECT t{$av_id[$i]}.stock_st_id " . $sql;
        $st_id = M()->query($sql);
        if (empty($st_id)) {
            echo 0;
        } else {
            echo $st_id[0]['stock_st_id'];
        }
        exit;
    }

    /**
     * 放大镜
     * @return [type] [description]
     */
    public function magnifier($goods_gid)
    {
        $db = M('g_goods_pic')->where('goods_gid='.$goods_gid)->all();
        if($db)
        {
            $data = array();
            foreach ($db  as  $k=>$v) 
            {            
                $data[$k][0] = __ROOT__.$v['small'];
                $data[$k][1] = __ROOT__.$v['medium'];
                $data[$k][2] = __ROOT__.$v['pic'];
            }
            $this->assign("file", $data);     
        }   
    }


    /**
     * 获取当前商品栏目和所属栏目
     * @param  [type] $goods_gid [description]
     * @return [type]            [description]
     */
    public function getCategory($goods_gid){
        $db = M('g_goods');
        $data = $db ->field('category_cid')->where('gid='.$goods_gid)->find();
        $cid = $data['category_cid'];
        $pidData = $db->table('g_category')->all();
        $data = Data::parentChannel($pidData,$cid);
        $data = array_reverse($data);
        $this->assign('pidData',$data);
    }
































}



